<?php

namespace App\Admin\Controllers;

use App\Models\Product;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ProductController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        $product = Product::firstOrCreate(["id"=>1]);

        return redirect(route('product.edit',['id'=>$product->id]));

    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form($id)->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');


            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Product::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id=0)
    {
        return Admin::form(Product::class, function (Form $form)use($id) {
            if ($id) {
                $form->replaceRedirectUrl(route('product.edit',$id));
            }
            $form->display('id', 'ID');
            $form->text("title",'商品名称');
            $form->text("price",'商品价格');
            $form->text("stock",'库存');
            $form->text("number",'已销售量');
            $form->text("shareTip",'分享提示语');
            $form->text("shipTip",'包邮提示语');
            $form->text("discountTip",'减免提示语');
            $form->textarea("des",'商品说明');
            $form->textarea("notice",'购机描述');
            $form->textarea("packageTip",'包装清单');
            $form->textarea("shouTip",'售后服务');
            $form->textarea("priceTip",'价格说明');
//            $form->image("image",'商品主图');
//            $form->multipleImage("image",'商品主图')->help("支持多图,jpg格式 250x250px");
            $form->textarea("info",'规格')->help("一行一个例如:颜色 红色");
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->saving(function ($form){
            });
        });
    }
}
